using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ebookreader.Resources;


namespace ebookreader
{
    public class ERMenu : Menu
    {
        //IInputManager input_manager;
        SpriteFont font, font_menu_sx;
        State currentState;
        private readonly float TRIAL_SPARKLE_RATE = 2;

        public ERMenu(Game game)
            : base(game)
        { }

        public override void Initialize()
        {
            currentState = Game.Services.GetService(typeof(State)) as State;

            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            //Game.Components.Add(new InputManager(Game));
            //input_manager = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            input_manager.OnTap += new InputManager.Tap(OnTap);

            base.Initialize();
        }

        Texture2D background, background_er, menu_button, menu_button_credits, menu_button_exit, backtrial, trial;

        protected override void LoadContent()
        {
            font = Game.Content.Load<SpriteFont>("Fonts/menu");
            font_menu_sx = Game.Content.Load<SpriteFont>("Fonts/menu_sx");
            background = Game.Content.Load<Texture2D>("Definitivo/background");
            background_er = Game.Content.Load<Texture2D>("todo/logo1");

            menu_button = Game.Content.Load<Texture2D>("Definitivo/menu_button");
            menu_button_credits = Game.Content.Load<Texture2D>("Definitivo/menu_button_credits");
            menu_button_exit = Game.Content.Load<Texture2D>("Definitivo/menu_button_exit");
            backtrial = Game.Content.Load<Texture2D>("Definitivo/backtrial");
            trial = Game.Content.Load<Texture2D>("Definitivo/trial");


            CreateButtonsList();
            LoadRenderableObjects();            



            base.LoadContent();
        }
                
        protected override void Dispose(bool disposing)
        {
            if(input_manager!=null)
                input_manager.OnTap -= new InputManager.Tap(OnTap);
            Game.Services.RemoveService(typeof(IInputManager));

            base.Dispose(disposing);
        }

        float toComponentstart = 0;
        public override void Update(GameTime gameTime)
        {
            toComponentstart += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (!EbookReader.state.hints[0] && toComponentstart > 1)
            {
                Game.Components.Add(new LargeMessagePopup(Game, Lang.Tip, Lang.TIP1));
                EbookReader.state.hints[0]=true;
            }

            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
                ScreenManager.ExitGame(Game);
            base.Update(gameTime);
        }

        void CreateButtonsList()
        {
            var first_button_pos = new Vector2(480,75); 
            int offset = 73;
            
            string[] labels;

            //Lang.MM_Credits, Lang.MM_Exit };
            if (currentState.currentBook != null)
            {
                labels = new string[] { Lang.MM_Resume, Lang.MM_Read, Lang.MM_DownM, Lang.MM_Options, };
                
            }
            else
            {
                labels = new string[] { Lang.MM_Read, Lang.MM_DownM, Lang.MM_Options };
            }
            

            entries = new List<MenuEntry>();

            for (int i = 0; i < labels.Length; i++)
            {
                
                float x = GraphicsDevice.Viewport.Width / 2.0f - font.MeasureString(labels[i]).X / 2.0f;

                MenuEntry e = new BoxedLabelButton(sprite_batch, menu_button, first_button_pos + Vector2.UnitY * offset * i, font, labels[i], Color.White, false, currentState.options);
                
                var index = currentState.currentBook == null ? i : i-1;
                e.OnPress += new Press(GetButtonEventHandler(index));

                entries.Add(e);   
            }      
            
      
            //CREDIT E EXIT
            var credit = new BoxedLabelButton(sprite_batch, menu_button_credits, new Vector2(116, 306), font_menu_sx, Lang.MM_Credits, Color.White, false, currentState.options);
            credit.OnPress+=new Press(GetButtonEventHandler(3));
            var exit = new BoxedLabelButton(sprite_batch, menu_button_exit, new Vector2(116, 364), font_menu_sx, Lang.MM_Exit, Color.White, false, currentState.options);
            exit.OnPress+=new Press(GetButtonEventHandler(4));
            entries.Add(credit);
            entries.Add(exit);

        }


        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin();
            sprite_batch.Draw(background, GraphicsDevice.Viewport.Bounds, Color.White);
            sprite_batch.Draw(background_er, new Vector2(63, 258), Color.White);
            if (EbookReader.isTrial)
            {
                sprite_batch.Draw(backtrial, new Vector2(181, 245), Color.White);
                sprite_batch.Draw(trial, new Vector2(195, 240), new Color(121, 123, 124)*0.8f);
                sprite_batch.Draw(trial, new Vector2(195, 240), new Color(255, 146, 18)  * (float)(Math.Abs(Math.Sin((double)gameTime.TotalGameTime.TotalSeconds*(MathHelper.PiOver2*TRIAL_SPARKLE_RATE)/2+0.5))));
            }
            sprite_batch.End();
            base.Draw(gameTime);
        }



        void LoadRenderableObjects()
        {
            renderables = new List<RenderableObject>();
        }

        Action<MenuEntry> GetButtonEventHandler(int index)
        {
            switch (index)
            {
                case -1:
                    return (MenuEntry m) => { ScreenManager.StartBookReader(Game, currentState.currentBook); };
                case 0:
                    return (MenuEntry m) => { ScreenManager.StartTitleList(Game, font); };
                case 2:
                    return (MenuEntry m) => { ScreenManager.StartOptionScreen(Game); };
                case 1:
                    return (MenuEntry m) => { ScreenManager.StartDownloadManager(Game, font); };
                case 3:
                    return (MenuEntry m) => { ScreenManager.StartCreditsScreen(Game); };
                case 4:
                    return (MenuEntry m) => { ScreenManager.ExitGame(Game); };
                default:
                    return (MenuEntry m) => { };
            }
        }
    }
}